//package wmmd.disc.webmaster;

import java.io.*;
//import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import db.DatabaseConnector;

public class ShowOneTopic extends HttpServlet {
    
    //----------------------------------------------------------------------------------------
    /**
     * Called by OneTopic.jsp - will display the selected topic in the specified table
     */
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        //The JSP document passes in which db table to use
        String tablename = request.getParameter("tName");
        
        int lRTT = 0;   //message's topic ID
        int CurrLevel = 0;  //current topic level
        int ELevels = 0;   //tracks number of list levels
        
        //Variables for the database connection
        DatabaseConnector con = null;
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        try {
            con = new DatabaseConnector(DatabaseConnector.getDatabaseType());
            //stmt = con.createStatement();
            
            //Get the topic to be expanded -- should never be 0
            lRTT = 0 + Integer.valueOf(request.getParameter("RTT")).intValue();
            if(lRTT == 0) {
                lRTT = 1;
            }
            
            //Search the table for the selected topic
            //TODO: Query
            String SQL = "SELECT ID,TLevel,Name,Date,Subject,Message FROM " + tablename + " WHERE Topic = ? ORDER BY ResponseID";
            String[][] rs = con.selectSQL(SQL,6,new Object[]{lRTT});
            
            //Assign ID and Date to string
            String id_string = rs[0][0];
            String nameStr = rs[0][2];
            String timeStamp = rs[0][3];
            String subjectStr = rs[0][4];
            String message_string = rs[0][5];
            /*String id_string = rs.getString("ID");
            String nameStr = rs.getString("Name");
            Timestamp timeStamp = rs.getTimestamp("Date");
            String subjectStr = rs.getString("Subject");
            String message_string = rs.getString("Message");*/
            
            // Format the date and username for the screen
            String dateStr = timeStamp;
            nameStr = nameStr.substring(0, nameStr.length()-2);
            
            out.println("<h3>" + lRTT + ". " + subjectStr + "</h3>");
            out.print("<p><em>started by <strong>" + nameStr + "</strong> on " + dateStr + "</em>");
            out.println(" &nbsp;<a href=\"displaymessage.jsp?RTID=" + id_string + "\">[ Respond ]</a></p>");
            out.print("<p>" + message_string.trim() + "</p>");
            
            out.println("<ul>");
            
            //Set the current topic level
            CurrLevel = 1;
            ELevels = 0;

            for (int i=1;i<rs.length;i++){//sentinel start at 1 correct?
                String id = rs[i][0];
                String tlevel = rs[i][1];
                String name = rs[0][2];
                String ts = rs[i][3];
                String subject = rs[i][4];
                String message = rs[i][5];
                /*String id = rs.getString("ID");
                String tlevel = rs.getString("TLevel");
                String name = rs.getString("Name");
                Timestamp ts = rs.getTimestamp("Date");
                String subject = rs.getString("Subject");
                String message = rs.getString("Message");*/
                
                //If the subject is longer than 50 chars, truncate it
                if(subject.length() > 50) {
                    subject = subject.substring(0, 50);
                }
                
                // Format the date and username for the screen
                String date = ts;
                name = name.substring(0, name.length()-2);
                
                // Print out the threaded topic
                int CurrLevel2 = Integer.valueOf(tlevel).intValue();
                if(CurrLevel2 > CurrLevel) {
                    // If the topic level is greater than the previous level,
                    // start a new nested list.
                    out.print("\n<ul>");
                    ELevels = ELevels + 1;
                    CurrLevel = CurrLevel2;
                } else if (CurrLevel2 < CurrLevel) {
                    // Return the ul list to main level
                    int jIndex = 0;
                    for(jIndex=(CurrLevel2 + 1); jIndex==CurrLevel; jIndex++) {
                        out.println("</li>");
                        out.println("</ul>");
                        ELevels = ELevels - 1;
                    }
                    CurrLevel = 1;
                }
                out.print("<li><strong>" + name + "</strong> posted <strong>" + subject + "</strong> on " + date);
                out.println(" &nbsp;<a href=\"displaymessage.jsp?RTID=" + id + "\">[ Respond ]</a><br><br>");
                out.println(message.trim() + "<br><br>");
            }
            //Return the UL list to the main level
            while(ELevels > 0) {
                out.println("</li>");
                out.println("</ul>");
                ELevels = ELevels - 1;
            }
            out.println("</ul>");
        }
        finally
        {
            con.CleanUp();
        }
    }
}
